package com.mdlib.helper.module.send.order.fragments;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.base.BaseTitleFragment;
import com.mdlib.helper.event.AgecyEvent;
import com.mdlib.helper.model.entity.AgecyType;
import com.mdlib.helper.ui.adapter.AgecyTypeAdapter;
import com.mdlib.helper.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 作者：HQY on 16/11/22 15:34
 * 邮箱：hqy_xz@126.com
 * 代办类型
 */

public class AgecyTypeFragment extends BaseTitleFragment {


    @Bind(R.id.rv_agecy_list)
    RecyclerView mRvAgecyList;

    private List<AgecyType> agecyTypeList = new ArrayList<>();

    private AgecyTypeAdapter mAgecyTypaAdapter;

    public static AgecyTypeFragment newInstance() {
        return new AgecyTypeFragment();
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_agecy_type;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        setTitle(R.string.agency_tv_type, R.color.white)
                .setTitleBgColor(R.color.generic_view_bg)
                .setImgLeftListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeFragment();
                    }
                });
        init();
    }

    public void init() {

        mAgecyTypaAdapter = new AgecyTypeAdapter(agecyTypeList);
        mRvAgecyList.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRvAgecyList.setAdapter(mAgecyTypaAdapter);
        mRvAgecyList.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {

            }

            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                super.onItemClick(adapter, view, position);
                removeFragment();
                AgecyType agecyType = agecyTypeList.get(position);
                EventBus.getDefault().postSticky(new AgecyEvent(agecyType));
            }
        });
        getAgecy();
    }


    private void getAgecy() {
//        startLoadingDialog(true);
        addSubscribe(DataManager.getInstance()
                .getUserApi().getAgecy()
                .compose(RxHelper.<ArrayList<AgecyType>>defaultHandleResult())
                .subscribe(new RxSubscribe<ArrayList<AgecyType>>() {
                    @Override
                    protected void onSucc(ArrayList<AgecyType> agecyTypes) {
//                        stopLoadDialog();
                        agecyTypeList = agecyTypes;
                        mAgecyTypaAdapter.setNewData(agecyTypeList);
                        mAgecyTypaAdapter.notifyDataSetChanged();
                    }

                    @Override
                    protected void onFail(String msg) {
//                        stopLoadDialog();
                        ToastUtil.showToast(msg);
                    }
                }));
    }

}
